package src.adiel.cameraApp;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.hardware.Camera;

/**
 * 
 * @author openmobster@gmail.com
 */
public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback
{
	private SurfaceHolder holder;
	private Camera camera;
	private int mFrameWidth;
	private int mFrameHeight;
	
	public CameraSurfaceView(Context context) 
	{
		super(context);
		
		//Initiate the Surface Holder properly
		this.holder = this.getHolder();
		this.holder.addCallback(this);
		this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}
	
	@Override
	public void surfaceCreated(SurfaceHolder holder) 
	{
		try
		{
			//Open the Camera in preview mode
			this.camera = Camera.open();
			this.camera.setPreviewDisplay(this.holder);
		}
		catch(IOException ioe)
		{
			ioe.printStackTrace(System.out);
		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
	{
        //Log.i(TAG, "surfaceCreated");
        /**
		if (camera != null) {
            Camera.Parameters params = camera.getParameters();
            List<Camera.Size> sizes = params.getSupportedPreviewSizes();
            mFrameWidth = width;
            mFrameHeight = height;

            // selecting optimal camera preview size
            {
                double minDiff = Double.MAX_VALUE;
                for (Camera.Size size : sizes) {
                    if (Math.abs(size.height - height) < minDiff) {
                        mFrameWidth = size.width;
                        mFrameHeight = size.height;
                        minDiff = Math.abs(size.height - height);
                    }
                }
            }

            params.setPreviewSize(getFrameWidth(), getFrameHeight());
            camera.setParameters(params);
            try {
            	camera.setPreviewDisplay(null);
			} catch (IOException e) {
				//Log.e(TAG, "mCamera.setPreviewDisplay fails: " + e);
			}
            camera.startPreview();
        }
		**/
		
		// Now that the size is known, set up the camera parameters and begin
		// the preview.
		Camera.Parameters parameters = camera.getParameters();
		parameters.setPreviewSize(width, height);
		camera.setParameters(parameters);
		camera.setDisplayOrientation(90);
		camera.startPreview();
	}


	@Override
	public void surfaceDestroyed(SurfaceHolder holder) 
	{
		// Surface will be destroyed when replaced with a new screen
		//Always make sure to release the Camera instance
		camera.stopPreview();
		camera.release();
		camera = null;
	}
	
	public Camera getCamera()
	{
		return this.camera;
	}
	
	public int getFrameWidth() {
        return mFrameWidth;
    }

    public int getFrameHeight() {
        return mFrameHeight;
    }
}
